package com.fancy.application.sys.config.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fancy.application.common.constant.Constants;
import com.fancy.application.common.service.impl.BaseService;
import com.fancy.application.sys.config.entity.SysConfigMain;
import com.fancy.application.sys.config.mapper.SysConfigMainMapper;
import com.fancy.application.sys.config.service.ISysConfigMainService;
import org.springframework.stereotype.Service;

/**
* fancycode自动生成v1.0
* @author wison
* 2019-11-27
*/

@Service("sysConfigMainService")
public class SysConfigMainServiceImpl extends BaseService<SysConfigMainMapper,SysConfigMain> implements ISysConfigMainService {

    /**
     * 获取初始化状态
     * @return
     */
    @Override
    public String getInitStatus() {
        return getValueByName(Constants.INIT_STATUS);
    }


    @Override

    public void updateInitStatus() {
        final String initStatus = "true";
        QueryWrapper<SysConfigMain> configMainQueryWrapper = new QueryWrapper<>();
        configMainQueryWrapper.eq("fd_name",Constants.INIT_STATUS);
        SysConfigMain sysConfigMain = this.getOne(configMainQueryWrapper);
        if(sysConfigMain!=null){
            sysConfigMain.setFdValue(initStatus);
            this.saveOrUpdate(sysConfigMain);
        }else{
            sysConfigMain = new SysConfigMain();
            sysConfigMain.setFdIsSystem(Boolean.TRUE);
            sysConfigMain.setFdName(Constants.INIT_STATUS);
            sysConfigMain.setFdValue(initStatus);
            sysConfigMain.setFdRemark("系统初始化键值");
            this.saveOrUpdate(sysConfigMain);
        }
    }

    private String getValueByName(String name) {
        String rtnValue = "";
        QueryWrapper<SysConfigMain> configMainQueryWrapper = new QueryWrapper<>();
        configMainQueryWrapper.eq("fd_name",name);
        SysConfigMain sysConfigMain = this.getOne(configMainQueryWrapper);
        if(sysConfigMain!=null){
            rtnValue = sysConfigMain.getFdValue();
        }
        return rtnValue;
    }


}
